package com.rk.recommand;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.Map;

public class UserCF_Step2 {
    public static class Step2Mapper extends Mapper<LongWritable, Text, Text, Text>{

        Text k = new Text();
        Text v = new Text();

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] words = CommonConst.DELIMITER.split(line);
            if (words.length >= 4){
                k.set(words[0] + "," + words[1]);
                v.set(words[2] + "," + words[3]);
                context.write(k, v);
            }
        }
    }

    public static class Step2Reducer extends Reducer<Text, Text, Text, Text>{

        Text v = new Text();

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            double sum = 0.0;
            double simliarity = 0.0;
            int num = 0;

            for (Text value : values){
                String line = value.toString();
                String[] words = CommonConst.DELIMITER.split(line);
                if(words.length >= 2){
                    sum += Math.pow((Double.parseDouble(words[0]) - Double.parseDouble(words[1])), 2);
                    num += 1;
                }
            }

            if(sum > 0.00000001){
                simliarity = (double) num / (1 + Math.sqrt(sum));
            }
            v.set(String.format("%.7f", simliarity));
            context.write(key, v);
        }
    }

    public static void run(Map<String, String> paths) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration configuration = new Configuration();
        System.setProperty("HADOOP_USER_NAME", "root");
        configuration.set("mapreduce.job.jar", "D:\\projects\\hadoop-study\\target\\hadoop-study-1.0-SNAPSHOT-jar-with-dependencies.jar");
        configuration.set("mapreduce.app-submission.cross-platform", "true");//意思是跨平台提交

        String input = paths.get("input_step2");
        String output = paths.get("output_step2");

        // 本地运行的话 无需在hdfs创建目录 否则需要使用hdfs API
        Job job = Job.getInstance(configuration, "UserCF_Step2 job");
        job.setJarByClass(UserCF_Step2.class);
        job.setMapperClass(Step2Mapper.class);
        job.setReducerClass(Step2Reducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.setInputPaths(job, new Path(input));
        FileOutputFormat.setOutputPath(job, new Path(output));

        boolean result = job.waitForCompletion(true);
        if(result){
            System.out.println("UserCF_Step2 run successful");
        }else {
            System.out.println("UserCF_Step2 run failed");
        }
    }
}
